package rr.core.display
{
	import flash.display.DisplayObject;
	
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	/**
	 * ...
	 * @author Raeef
	 */
	
	public class Align
	{
		public static const TOP_LEFT	 :String = 'top_left';
		public static const TOP_CENTER	 :String = 'top_center';
		public static const TOP_RIGHT	 :String = 'top_right';
		
		public static const MIDDLE_LEFT	 :String = 'middle_left';
		public static const MIDDLE_CENTER:String = 'middle_center';
		public static const MIDDLE_RIGHT :String = 'middle_right';
		
		public static const BOTTOM_LEFT	 :String = 'bottom_left';
		public static const BOTTOM_CENTER:String = 'bottom_center';
		public static const BOTTOM_RIGHT :String = 'bottom_right';
		
		public static function getDisObjAlignPoint(disObjWidth:uint, disObjHeight:uint, containerWidth:uint, containerHeight:uint, align:String = 'top_left'):Point
		{
			var alignPoint:Point = new Point();
			
			switch(align)
			{
				case TOP_CENTER:
					alignPoint.x = (containerWidth - disObjWidth) / 2;
				break;
				
				case TOP_RIGHT:
					alignPoint.x = containerWidth - disObjWidth;
				break;
				
				case MIDDLE_LEFT:
					alignPoint.y = (containerHeight - disObjHeight) / 2;
				break;
				
				case MIDDLE_CENTER:
					alignPoint.x = (containerWidth - disObjWidth) / 2;
					alignPoint.y = (containerHeight - disObjHeight) / 2;
				break;
				
				case MIDDLE_RIGHT:
					alignPoint.x = containerWidth - disObjWidth;
					alignPoint.y = (containerHeight - disObjHeight) / 2;
				break;
				
				case BOTTOM_LEFT:
					alignPoint.y = containerHeight - disObjHeight;
				break;
				
				case BOTTOM_CENTER:
					alignPoint.x = (containerWidth - disObjWidth) / 2;
					alignPoint.y = containerHeight - disObjHeight;
				break;
				
				case BOTTOM_RIGHT:
					alignPoint.x = containerWidth - disObjWidth;
					alignPoint.y = containerHeight - disObjHeight;
				break;
			}
			
			return alignPoint;
		}
		
		public static function setDisObjAlign(disObj:DisplayObject, containerWidth:uint, containerHeight:uint, align:String = 'top_left', useOriginalDimensions:Boolean = false):void
		{
			var nativeX		:Number = disObj.getBounds(disObj).x;
			var nativeY		:Number = disObj.getBounds(disObj).y;
			var disObjWidth	:uint	= 0;
			var disObjHeight:uint	= 0;
			
			if (useOriginalDimensions)
			{
				disObjWidth  = disObj.getRect(disObj).width;
				disObjHeight = disObj.getRect(disObj).height;
			}
			else
			{
				disObjWidth  = disObj.width;
				disObjHeight = disObj.height;
			}
			
			switch(align)
			{
				case TOP_LEFT:
					disObj.x -= nativeX;
					disObj.y -= nativeY;
				break;
				
				case TOP_CENTER:
					disObj.x = (containerWidth - disObjWidth) / 2 - nativeX;
					disObj.y -= nativeY;
				break;
				
				case TOP_RIGHT:
					disObj.x = containerWidth - disObjWidth - nativeX;
					disObj.y -= nativeY;
				break;
				
				case MIDDLE_LEFT:
					disObj.x -= nativeX;
					disObj.y = (containerHeight - disObjHeight) / 2 - nativeY;
				break;
				
				case MIDDLE_CENTER:
					disObj.x = (containerWidth - disObjWidth) / 2 - nativeX;
					disObj.y = (containerHeight - disObjHeight) / 2 - nativeY;
				break;
				
				case MIDDLE_RIGHT:
					disObj.x = containerWidth - disObjWidth - nativeX;
					disObj.y = (containerHeight - disObjHeight) / 2 - nativeY;
				break;
				
				case BOTTOM_LEFT:
					disObj.x -= nativeX;
					disObj.y = containerHeight - disObjHeight - nativeY;
				break;
				
				case BOTTOM_CENTER:
					disObj.x = (containerWidth - disObjWidth) / 2 - nativeX;
					disObj.y = containerHeight - disObjHeight - nativeY;
				break;
				
				case BOTTOM_RIGHT:
					disObj.x = containerWidth - disObjWidth - nativeX;
					disObj.y = containerHeight - disObjHeight - nativeY;
				break;
			}
		}
		
		public static function setDisObjPivot(disObj:DisplayObject, pivot:String = "top_left", useOriginalDimensions:Boolean = false):void
		{
			var disObjWidth	:uint = 0;
			var disObjHeight:uint = 0;
			
			if (useOriginalDimensions)
			{
				disObjWidth  = disObj.getRect(disObj).width;
				disObjHeight = disObj.getRect(disObj).height;
			}
			else
			{
				disObjWidth  = disObj.width;
				disObjHeight = disObj.height;
			}
			
			switch(pivot)
			{
				case TOP_LEFT:
					disObj.x = 0;
					disObj.y = 0;
				break;
				
				case TOP_CENTER:
					disObj.x = -disObjWidth / 2;
					disObj.y = 0;
				break;
				
				case TOP_RIGHT:
					disObj.x = -disObjWidth;
					disObj.y = 0;
				break;
				
				case MIDDLE_LEFT:
					disObj.x = 0;
					disObj.y = -disObjHeight / 2;
				break;
				
				case MIDDLE_CENTER:
					disObj.x = -disObjWidth / 2;
					disObj.y = -disObjHeight / 2;
				break;
				
				case MIDDLE_RIGHT:
					disObj.x = -disObjWidth;
					disObj.y = -disObjHeight / 2;
				break;
				
				case BOTTOM_LEFT:
					disObj.x = 0;
					disObj.y = -disObjHeight;
				break;
				
				case BOTTOM_CENTER:
					disObj.x = -disObjWidth / 2;
					disObj.y = -disObjHeight;
				break;
				
				case BOTTOM_RIGHT:
					disObj.x = -disObjWidth;
					disObj.y = -disObjHeight;
				break;
			}
		}
	}
}